package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlCjllsjmhjlbBaDetailBo;
import com.ruoyi.report_jl.domain.vo.ReportJlCjllsjmhjlbBaDetailVo;
import com.ruoyi.report_jl.domain.ReportJlCjllsjmhjlbBaDetail;
import com.ruoyi.report_jl.mapper.ReportJlCjllsjmhjlbBaDetailMapper;
import com.ruoyi.report_jl.service.IReportJlCjllsjmhjlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粗集料磨耗记录详情Service业务层处理
 *
 * @author tll
 * @date 2024-05-23
 */
@RequiredArgsConstructor
@Service
public class ReportJlCjllsjmhjlbBaDetailServiceImpl implements IReportJlCjllsjmhjlbBaDetailService {

    private final ReportJlCjllsjmhjlbBaDetailMapper baseMapper;

    /**
     * 查询粗集料磨耗记录详情
     */
    @Override
    public ReportJlCjllsjmhjlbBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询粗集料磨耗记录详情列表
     */
    @Override
    public TableDataInfo<ReportJlCjllsjmhjlbBaDetailVo> queryPageList(ReportJlCjllsjmhjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlCjllsjmhjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportJlCjllsjmhjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粗集料磨耗记录详情列表
     */
    @Override
    public List<ReportJlCjllsjmhjlbBaDetailVo> queryList(ReportJlCjllsjmhjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportJlCjllsjmhjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlCjllsjmhjlbBaDetail> buildQueryWrapper(ReportJlCjllsjmhjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlCjllsjmhjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportJlCjllsjmhjlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getLuosjmhsycs()), ReportJlCjllsjmhjlbBaDetail::getLuosjmhsycs, bo.getLuosjmhsycs());
        lqw.eq(bo.getLuosjmhsyljzc() != null, ReportJlCjllsjmhjlbBaDetail::getLuosjmhsyljzc, bo.getLuosjmhsyljzc());
        lqw.eq(bo.getLuosjmhsysyzzl() != null, ReportJlCjllsjmhjlbBaDetail::getLuosjmhsysyzzl, bo.getLuosjmhsysyzzl());
        lqw.eq(bo.getLuosjmhsydy17skzl() != null, ReportJlCjllsjmhjlbBaDetail::getLuosjmhsydy17skzl, bo.getLuosjmhsydy17skzl());
        lqw.eq(bo.getLuosjmhsymhcz() != null, ReportJlCjllsjmhjlbBaDetail::getLuosjmhsymhcz, bo.getLuosjmhsymhcz());
        lqw.eq(bo.getLuosjmhsymhczPjz() != null, ReportJlCjllsjmhjlbBaDetail::getLuosjmhsymhczPjz, bo.getLuosjmhsymhczPjz());
        return lqw;
    }

    /**
     * 新增粗集料磨耗记录详情
     */
    @Override
    public Boolean insertByBo(ReportJlCjllsjmhjlbBaDetailBo bo) {
        ReportJlCjllsjmhjlbBaDetail add = BeanUtil.toBean(bo, ReportJlCjllsjmhjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改粗集料磨耗记录详情
     */
    @Override
    public Boolean updateByBo(ReportJlCjllsjmhjlbBaDetailBo bo) {
        ReportJlCjllsjmhjlbBaDetail update = BeanUtil.toBean(bo, ReportJlCjllsjmhjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlCjllsjmhjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粗集料磨耗记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
